package com.yzw.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yzw.common.domain.EduChapter;
import com.yzw.common.domain.EduVideo;
import com.yzw.common.dto.R;
import com.yzw.common.vo.ChapterVo;
import com.yzw.course.service.EduChapterService;
import com.yzw.course.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Yzw
 * @Description TODO
 * @Date 2023/5/11 13:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/chapter")
public class EduChapterController {
    @Resource
    private EduChapterService eduChapterService;
    @Resource
    private EduVideoService eduVideoService;

    @ApiOperation("根据课程id查询课程的章节及小节")
    @GetMapping("/course/{courseId}")
    public R selChapter(@PathVariable String courseId) {
        List<ChapterVo> list = eduChapterService.selChapterVideoByCourseId(courseId);
        return R.success("查询成功",list);
    }


    @ApiOperation("添加课程章节")
    @PostMapping ("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return R.success("添加成功",null);
    }

    @ApiOperation("根据课程id查询课程章节")
    @GetMapping ("/getChapterById/{chapterId}")
    public R getChapterById(@PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.success("查询成功",eduChapter);
    }

    @ApiOperation("修改课程章节")
    @PostMapping ("/updChapter")
    public R updChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return R.success("修改成功",null);
    }

    @Transactional
    @ApiOperation("删除课程章节")
    @DeleteMapping ("{chapterId}")
    public R delChapter(@PathVariable String chapterId) {
        LambdaQueryWrapper<EduChapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduChapter::getId,chapterId);
        eduChapterService.remove(queryWrapper);
        LambdaQueryWrapper<EduVideo> queryWrapperVideo = new LambdaQueryWrapper<>();
        queryWrapperVideo.eq(EduVideo::getChapterId,chapterId);
        eduVideoService.remove(queryWrapperVideo);
        return R.success("删除成功",null);
    }
}
